<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>json - 非常重要</title>
</head>
<body>
    <script>
        //json是轻量级的数据交互的格式.
        //后端(java,python) => 返回json格式的数据 => 前端进行解析.

        //定义一个单个json对象
        let obj1 = {"id":10,"name":"tom"};
        //通过json对象.key来获取value
        console.log(obj1.id);

        //定义一个json数组
        let arr = [{"id":10,"name":"tom"},{"id":20,"name":"admin"}];
        console.log(arr[1].name);//admin

        //定义一个json字符串 - 单个对象
        let str1 =  '{"id":10,"name":"tom"}';
        //let str1 =  "{'id':10,'name':'tom'}";
        //json字符串转成json对象
         let str1_to_json = eval('('+str1+")");
         console.log(str1_to_json.name);

        //推荐用这个
        let s = JSON.parse(str1);
        console.log(s);
        console.log(s.id);

        //json字符串数组
        let str_arr = '[{"id":10,"name":"tom"},{"id":20,"name":"admin"}]';
        console.log(eval(str_arr));

        console.log(JSON.parse(str_arr));

        //json对象=>json字符串
        console.log(JSON.stringify(arr,null,"\t"));

        //结构稍微复杂一点的结构
        let mm = {
            msg:"查询xx信息",
            code:200,
            data:[
                {"id":10,"name":"tom"},
                {"id":20,"name":"admin"}
            ]
        }
        console.log(mm.data[1].name);

    </script>
</body>
</html>